/* arch/i386/boot/setup.S 
 * 
 * This file is part of xnix. 
 * 
 * XNIX is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * XNIX is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with XNIX. If not, see <https://www.gnu.org/licenses/>
 */ 




.code16


/* switch to the protected-mode, and to real system (XNIX Loader) */

.globl _start

_start:	xorw %ax, %ax

	movw %ax, %ds
	movw %ax, %es
	movw %ax, %ss

	/* yeah, we should move it again. */
	movw $0x7e00, %di
	movw $0x8000, %si

	movw $512, %cx

	cld
	rep
	movsb

	movw $0x7c00, %sp

	ljmp $0, $real_start

real_start:
	/* open a20 */
	cli

	inb $0x92, %al
	orb $0x02, %al
	outb %al, $0x92

	/* setup idt and gdt */
	lgdt gdt32_descr

	movl %cr0, %eax
	orl $1, %eax

	/* that's it! */
	movl %eax, %cr0

	/* setup paging */

	movl $0, %edi
	movl $512, %ecx
	xorl %eax, %eax

	cld

rsetpt:	rep
	stosl

	movl $0x00001007, 0
	movl $0x00001007, 0xc00
	
	movl $0x1000, %edi
	movl $1024, %ecx
	movl $0x00000007, %eax

rsetp:	stosl
	addl $4096, %eax

	loop rsetp

	xorl %eax, %eax
	movl %eax, %cr3

	movl %cr0, %eax
	orl $0x80000000, %eax
	movl %eax, %cr0

	ljmp $0x08, $init_prot


.code32


init_prot:
	movl $0x10, %eax

	movl %eax, %ds
	movl %eax, %es
	movl %eax, %fs
	movl %eax, %gs

	movl %eax, %ss

	movl $0x7c00, %esp

	/* move it to 1mb */

	movl $1048576, %edi
	movl $0x8400, %esi

	movl $131072, %ecx

	cld

	rep
	movsl

	ljmp $0x08, $1048576


.globl gdt32

gdt32:	.long 0x00000000, 0x00000000
	.long 0x0000ffff, 0x00cf9a00
	.long 0x0000ffff, 0x00cf9200


.globl gdt32_descr

gdt32_descr:
	.word . - gdt32
	.long gdt32

